package service

import (
	"context"
	"errors"
	"ruoyi-go-echo-ddd/internal/domain/model"
	"ruoyi-go-echo-ddd/internal/domain/repository"
	"ruoyi-go-echo-ddd/internal/infrastructure/http/request"
)

type SysUserService struct {
	repo repository.SysUserRepository
}

func NewSysUserService(repo repository.SysUserRepository) *SysUserService {
	return &SysUserService{
		repo: repo,
	}
}

func (s *SysUserService) Insert(ctx context.Context, user *model.User) (*model.User, error) {
	return s.repo.Insert(ctx, user)
}

func (s *SysUserService) Update(ctx context.Context, user *model.User) (int64, error) {
	return s.repo.Update(ctx, user)
}

func (s *SysUserService) SelectById(ctx context.Context, id int64) (*model.User, error) {
	if id == 0 {
		return nil, errors.New("用户ID不能为空")
	}
	return s.repo.SelectById(ctx, id)
}
func (s *SysUserService) SelectByUsername(ctx context.Context, username string) (*model.User, error) {
	if username == "" {
		return nil, errors.New("用户名不能为空")
	}
	return s.repo.SelectByUsername(ctx, username)
}

func (s *SysUserService) DeleteById(ctx context.Context, id int64) (int64, error) {
	if id == 0 {
		return 0, errors.New("删除参数验证失败")
	}
	return s.repo.DeleteById(ctx, id)
}

func (s *SysUserService) BatchInsert(ctx context.Context, list []*model.User) ([]int64, error) {
	if len(list) <= 0 {
		return nil, errors.New("系统用户批量新增参数验证失败")
	}
	return s.repo.BatchInsert(ctx, list)
}

func (s *SysUserService) BatchDelete(ctx context.Context, ids []any) (int64, error) {
	if len(ids) <= 0 {
		return 0, errors.New("系统用户批量删除参数验证失败")
	}
	return s.repo.BatchDelete(ctx, ids)
}

func (s *SysUserService) SelectPage(ctx context.Context, param *request.UserPageParam) ([]*model.User, int64, int64, error) {
	return s.repo.SelectPage(ctx, param)
}

func (s *SysUserService) SelectList(ctx context.Context, limit, offset int64) ([]*model.UserForExcel, error) {
	return s.repo.SelectList(ctx, limit, offset)
}

func (s *SysUserService) GetTotalCount(ctx context.Context) (int64, error) {
	return s.repo.GetTotalCount(ctx)
}

func (s *SysUserService) Login(ctx context.Context, user *model.UserLogin) (*model.User, error) {
	return s.repo.Login(ctx, user)
}

func (s *SysUserService) SelectAll(ctx context.Context) ([]*model.UserForExcel, error) {
	return s.repo.SelectAll(ctx)
}
